﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;

namespace codeding.KMeans.DataStructures.Lists
{
    public class Ratings : DoiTuong
    {
        #region int RatingID property
        public int RatingID
        {
            get
            {
                if (_DataRow["RatingID"] is int) return (int)_DataRow["RatingID"];
                return -1;
            }
            set
            {
                _DataRow["RatingID"] = value;
            }
        }
        #endregion

        #region int UserID property
        public int UserID
        {
            get
            {
                if (_DataRow["UserID"] is int) return (int)_DataRow["UserID"];
                return -1;
            }
            set
            {
                _DataRow["UserID"] = value;
            }
        }
        #endregion

        #region int MovieID property
        public int MovieID
        {
            get
            {
                if (_DataRow["MovieID"] is int) return (int)_DataRow["MovieID"];
                return -1;
            }
            set
            {
                _DataRow["MovieID"] = value;
            }
        }
        #endregion

        #region double Rating property
        public double Rating
        {
            get
            {
                if (_DataRow["Rating"] is double) return (double)_DataRow["Rating"];
                return -1;
            }
            set
            {
                _DataRow["Rating"] = value;
            }
        }
        #endregion

        #region bool Status property
        public bool Status
        {
            get
            {
                if (_DataRow["Status"] is bool) return (bool)_DataRow["Status"];
                return true;
            }
            set
            {
                _DataRow["Status"] = value;
            }
        }
        #endregion

        public Ratings(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucRatings : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucRatings _Instance;
        public static DanhMucRatings Instance
        {
            get
            {
                //if (_Instance == null)
                //{
                    _Instance = new DanhMucRatings();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    _Instance.DataView.Sort = "";
                //}

                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "tblRating";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new Ratings((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public Ratings this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new Ratings(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public Ratings NewDoiTuong()
        {
            Ratings doiTuong = new Ratings(DataTable.NewRow());
            return doiTuong;
        }

        public Ratings GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new Ratings(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public Ratings GetByID(int objID)
        {
            return GetByProperty("RatingID", objID);
        }
        public Ratings GetByTen(string ten)
        {
            return GetByProperty("MovieName", ten);
        }

        public DanhMucRatings Filter(string filterExpression)
        {
            try
            {
                DanhMucRatings obj = new DanhMucRatings();
                obj.DataView = new DataView(DanhMucRatings.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static DanhMucRatings GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}